package fpt.gui;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import fpt.graph.*;
import fpt.myFileHandler.*;


/**
 * mainframe
 * 
 * r41
 * 
 * 12 March 2010
 * 
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 * 
 * This class creates the GUI used for the FPT Algorithm project.
 * @author Daniel De Guzman
*/
public class mainframe extends javax.swing.JFrame {
	private JButton btnOpenFile;
	private JTextArea txtOutput;
	private AbstractAction actSave;
	private AbstractAction actClear;
	private JButton btnClear;
	private JButton btnSave;
	private JScrollPane jScrollPane1;
	private AbstractAction actOpen;
	private JFileChooser fc;
	
	/** Auto-generated main method to display this JFrame */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				mainframe inst = new mainframe();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	/** Auto-generated constructor */
	public mainframe() {
		super();
		initGUI();
	}
	
	/** Auto-generated when components are added in using GUI Editor. */
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			this.setResizable(false);
			this.setTitle("FPT Algorithm");
			{
				btnOpenFile = new JButton();
				btnOpenFile.setText("Open...");
				btnOpenFile.setAction(getActOpen()); 
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(getJScrollPane1(), GroupLayout.PREFERRED_SIZE, 252, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(getbtnSave(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(btnOpenFile, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(getBtnClear(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(19, 19));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(getJScrollPane1(), GroupLayout.PREFERRED_SIZE, 540, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(292)
				        .addComponent(getBtnClear(), GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addComponent(getbtnSave(), GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addComponent(btnOpenFile, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 27, Short.MAX_VALUE)))
				.addContainerGap(24, 24));
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private AbstractAction getActSave() {
		if(actSave == null) {
			actSave = new AbstractAction("Save", null) {
				public void actionPerformed(ActionEvent evt) {
					fc = new JFileChooser();
					fc.setCurrentDirectory(new java.io.File("."));
					if (evt.getSource() == btnSave){
						int returnVal = fc.showSaveDialog(mainframe.this);
						if (returnVal == JFileChooser.APPROVE_OPTION){
							File file = fc.getSelectedFile();
							String s = txtOutput.getText();
							
							try {
								FileOutputStream fs = new FileOutputStream(file + ".rtf");
								fs.write(s.getBytes());
								fs.close();
							} catch (Exception e) {
								e.printStackTrace();
							}
							
						} else {
							txtOutput.append(Messages.errorSave);
						}
					}
				}
			};
		}
		return actSave;
	}
	
	/**
	 * Auto-generated when AbstractAction component was added using GUI Editor. Opens a file.
	 * @return
	 */
	private AbstractAction getActOpen() {
		if(actOpen == null) {
			actOpen = new AbstractAction("Open", null) {
				public void actionPerformed(ActionEvent evt) {
					fc = new JFileChooser();
					fc.setCurrentDirectory(new java.io.File("."));
					if (evt.getSource() == btnOpenFile){
						int returnVal = fc.showOpenDialog(mainframe.this);
						if (returnVal == JFileChooser.APPROVE_OPTION){
							openFile();
						} else {
							txtOutput.append(Messages.errorOpen);
						} // end if returnVal
					} // end if evt.get...
				} // end public void
			}; // end actOpen
		} // end if actOpen == null
		return actOpen;
	}

	/**
	 * Opens and processes a file depending on its extension
	 */
	private void openFile(){
		File file = fc.getSelectedFile();
		String fileName = file.getName();
		
		try{
			FileReader in = new FileReader(file);
			txtOutput.setText("");
			//txtOutput.append(Messages.fpFileName(fileName));
			
			// Choose how to handle the file
			Handler myFileHandler = null;
			if (fileName.endsWith(".rtf")){
				myFileHandler = new RTFHandler(in);
			} 
			else if (file.getName().endsWith(".txt")){
				myFileHandler = new TXTHandler(in);
			} else {
				txtOutput.append(Messages.errorExt);
			} // end if file.getName
			
			// Start processing the file
			if (!myFileHandler.equals(null) && Functions.processFile(myFileHandler)){
				//txtOutput.append(Functions.debuggingFileProcessing(myFileHandler));
				GraphFPT graphInfo = new GraphFPT(myFileHandler);
				//txtOutput.append(Functions.debuggingCreateGraph("Created Graph: ", graphInfo));
				
				GraphFPT kernelizedGraph = Functions.kernelize(graphInfo);
				//txtOutput.append(Functions.debuggingCreateGraph("Kernelized Graph: ",kernelizedGraph));
				
				SearchTreeNode rootNode = new SearchTreeNode("Root Node",kernelizedGraph);
				Functions.boundedSearch(rootNode);
				txtOutput.append(Functions.debuggingBoundedSearchTree(rootNode));
			} else {
				txtOutput.append(myFileHandler.getError());
			}	
			in.close();
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	/** Auto-generated */
	private JTextArea getTxtOutput() {
		if(txtOutput == null) {
			txtOutput = new JTextArea();
			txtOutput.setEditable(false);
			txtOutput.setFont(new java.awt.Font("Courier New",0,16));
			txtOutput.setText(Messages.instructions);
		}
		return txtOutput;
	}
	
	/** Auto-generated */
	private JScrollPane getJScrollPane1() {
		if(jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setViewportView(getTxtOutput());
		}
		return jScrollPane1;
	}

	private JButton getbtnSave() {
		if(btnSave == null) {
			btnSave = new JButton();
			btnSave.setText("Save");
			btnSave.setAction(getActSave());
		}
		return btnSave;
	}
	
	private JButton getBtnClear() {
		if(btnClear == null) {
			btnClear = new JButton();
			btnClear.setText("Clear");
			btnClear.setAction(getActClear());
		}
		return btnClear;
	}
	
	private AbstractAction getActClear() {
		if(actClear == null) {
			actClear = new AbstractAction("Clear", null) {
				public void actionPerformed(ActionEvent evt) {
					txtOutput.setText("");
				}
			};
		}
		return actClear;
	}
}
